package com.hsytech.dijiebao.home.fragment;

import java.util.ArrayList;
import java.util.List;

import com.deparse.dpframe.utils.DPLogUtil;
import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.bean.HomeTaxiBean;
import com.hsytech.dijiebao.home.activity.HomeDetailActivity;
import com.hsytech.dijiebao.home.activity.MoreActivity;
import com.hsytech.dijiebao.home.alldetail.MyselfTaxiDetailActivity;
import com.hsytech.dijiebao.home.alldetail.TaxiDetailActivity;
import com.hsytech.dijiebao.utils.Constants;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TaxiFrament extends Fragment implements OnClickListener {

    public void setData(List<HomeTaxiBean> zuTaxiList, List<HomeTaxiBean> myTaxiList) {

        this.zuTaxiList = zuTaxiList;
        this.myTaxiList = myTaxiList;
    }

    private LinearLayout qiu_zu_car_title, my_car_title;
    private View root;
    private List<HomeTaxiBean> zuTaxiList=new ArrayList<>();
    private List<HomeTaxiBean> myTaxiList=new ArrayList<>();
    private LayoutInflater inflater;
    private Adapter zuAdapter;
    private Adapter myAdapter;
    private ListView zu_car_listView, my_car_listView;

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        DPLogUtil.d("TaxiFrament onCreateView");
        root = inflater.inflate(R.layout.fragment_home_taxi, container, false);
        this.inflater = LayoutInflater.from(getActivity());
        zu_car_listView = (ListView) root.findViewById(R.id.zu_car_listView);
        my_car_listView = (ListView) root.findViewById(R.id.my_car_listView);
        qiu_zu_car_title = (LinearLayout) root.findViewById(R.id.qiu_zu_car_title);
        my_car_title = (LinearLayout) root.findViewById(R.id.my_car_title);

        qiu_zu_car_title.setOnClickListener(this);
        my_car_title.setOnClickListener(this);

        zuAdapter = new Adapter(zuTaxiList);
        myAdapter = new Adapter(myTaxiList);

        zu_car_listView.setAdapter(zuAdapter);
        my_car_listView.setAdapter(myAdapter);

        zu_car_listView.setOnItemClickListener(new MyListOnItemClick(zuTaxiList));
        my_car_listView.setOnItemClickListener(new MyListOnItemClick(myTaxiList));

        return root;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.qiu_zu_car_title) {
            Intent intent = new Intent(getActivity(), MoreActivity.class);
            intent.putExtra("title", "求租找车");
            intent.putExtra("type", "1");
            intent.putExtra("url", Constants.HTTP_MORE_TAXI);
            startActivity(intent);

        } else if (v.getId() == R.id.my_car_title) {
            Intent intent = new Intent(getActivity(), MoreActivity.class);
            intent.putExtra("title", "我有车租");
            intent.putExtra("type", "1");
            intent.putExtra("url", Constants.HTTP_MORE_MY_TAXI);
            startActivity(intent);

        }

    }

    private class MyListOnItemClick implements ListView.OnItemClickListener {

        private List<HomeTaxiBean> list;

        public MyListOnItemClick(List<HomeTaxiBean> list) {
            this.list = list;
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), HomeDetailActivity.class);
            intent.putExtra("id", list.get(position).getId());
            intent.putExtra("type", "2");
            startActivity(intent);

        }
    }


    private class Adapter extends BaseAdapter {

        private List<HomeTaxiBean> list;

        public Adapter(List<HomeTaxiBean> list) {
            this.list = list;
        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = inflater.inflate(R.layout.item_home_list, null);
            TextView city_name = (TextView) convertView.findViewById(R.id.city_name);
            TextView content = (TextView) convertView.findViewById(R.id.content);
            TextView publisher_name = (TextView) convertView.findViewById(R.id.publisher_name);
            ImageView hot = (ImageView) convertView.findViewById(R.id.hot);
            ImageView recommend = (ImageView) convertView.findViewById(R.id.recommend);
            ImageView news = (ImageView) convertView.findViewById(R.id.news);

            city_name.setText(list.get(position).getCity_name());
            city_name.setTextColor(Color.parseColor(list.get(position).getCity_color().replace("0x", "#")));
            content.setText(list.get(position).getCar_cat_name() + " " + list.get(position).getStart_time() + "-"
                    + list.get(position).getEnd_time() + " " + getString(R.string.base_chen_che) + list.get(position).getCarry_passenger_num() + getString(R.string.base_ren));
            publisher_name.setText(getString(R.string.base_fa_bu_ren) + " " + list.get(position).getPublisher_name());

            if (list.get(position).getIs_hot().equals("1")) {
                hot.setVisibility(View.VISIBLE);
            } else {
                hot.setVisibility(View.GONE);
            }
            if (list.get(position).getIs_new().equals("1")) {
                news.setVisibility(View.VISIBLE);
            } else {
                news.setVisibility(View.GONE);
            }
            if (list.get(position).getIs_recommend().equals("1")) {
                recommend.setVisibility(View.VISIBLE);
            } else {
                recommend.setVisibility(View.GONE);
            }

            return convertView;
        }
    }

}
